import random


class mySort:
    def __init__(self, arr_len):
        # self.arr = [3, 87, 2, 93, 78, 56, 61, 38, 12, 40]
        self.arr=[]
        self.arr_len = arr_len
        self.temp_arr = [0] * self.arr_len

    def random_int(self):
        for i in range(self.arr_len):
            self.arr.append(random.randint(0, 99))

    def test_sort(self, sort_method, *args, **kwargs):
        print(self.arr)
        sort_method(*args, **kwargs)
        print(self.arr)
    # 合并两个数组
    def merge(self, low, mid, high):
        temp_arr = self.temp_arr
        arr = self.arr
        temp_arr[low:high + 1] = arr[low:high + 1]
        i = low
        j = mid + 1
        k = low
        while i <= mid and j <= high:
            if temp_arr[i] < temp_arr[j]:
                arr[k]=temp_arr[i]
                i += 1
            else:
                arr[k]=temp_arr[j]
                j += 1
            k += 1
        while i <= mid:
            arr[k]=temp_arr[i]
            i += 1
            k += 1
        while j <= high:
            arr[k]=temp_arr[j]
            j += 1
            k += 1

    def merge_sort(self, low, high):
        if low < high:
            mid = (low + high) // 2
            self.merge_sort(low, mid)
            self.merge_sort(mid + 1, high)
            self.merge(low, mid, high)


if __name__ == '__main__':
    temp = mySort(10)
    temp.random_int()
    temp.test_sort(temp.merge_sort,0,temp.arr_len-1)
